﻿$(function() {
	const now = Date.now();
	const ts = Math.ceil(now / 1000);
	$('#dnTs').val(ts);
	$('#dnMts').val(now);
	$('#dnRandStr').val();
	$('#genRandStrBtn').click(genStr);
	$('#copyRandStrBtn').click(copyIptVal);
	$('.copyBtn').click(copyIptVal);
	genStr();
	$('#MD5Raw').on('keyup', genMD5);
});

const chars = {
	num: '0123456789',
	lower: 'abcdefghijklmnopqrstuvwxtz',
	upper: 'ABCDEFGHIJKLMNOPQRSTUVWXTZ',
	lowerNum: '0123456789abcdefghijklmnopqrstuvwxtz',
	upperNum: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ',
	default: '0123456789abcdefghiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXTZ',
	safe: '2346789ABCDEFGHJKLMNPRTUVWXTZ',
};

function random(len, srcChars) {
	let str = '';
	srcChars = srcChars || chars.default;
	for (let i = 0; i < len; i++) {
		str += srcChars.charAt(Math.floor(Math.random() * srcChars.length));
	}
	return str;
}

function genStr() {
	let num = $('#randStrLen').val();
	if(!(num > 0)) {
		num = 16;
		$('#randStrLen').val(num);
	}
	const str = random(num, chars.lowerNum);
	$('#dnRandStr').val(str);
}

function copyIptVal(evt) {
	const iptId = $(evt.target).data('for');
	const ipt = document.getElementById(iptId);
	ipt.select();
	document.execCommand("Copy");
	const toastHandle = document.getElementById('copySuccessToast');
	const toast = new bootstrap.Toast(toastHandle, {
		delay: 3000
	});
	toast.show();
}

let th = null;

function genMD5(evt) {
	clearTimeout(th);
	th = setTimeout(()=>{
		const $ipt = $(evt.target);
		$('#dnMD5Val').val(febs.crypt.md5($ipt.val()));
	}, 500);
}